package com.jjkj.environmental.environmental.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.text.Editable
import com.jjkj.environmental.environmental.R
import com.jjkj.environmental.environmental.adapter.CheckAdapter
import com.jjkj.environmental.environmental.bean.model.CarInfo
import com.jjkj.environmental.environmental.bean.model.CheckInfo
import com.jjkj.environmental.environmental.bean.model.CheckItem
import com.jjkj.environmental.environmental.presenter.CheckPresenter
import com.jjkj.environmental.environmental.ui.base.BaseActivity
import com.jjkj.environmental.environmental.ui.dialog.FuelChooseDialog
import com.jjkj.environmental.environmental.utils.MyLog
import com.jjkj.environmental.environmental.utils.MyTextWatcher
import com.jjkj.environmental.environmental.view.CheckView
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import kotlinx.android.synthetic.main.activity_check.*
import java.util.*


class CheckActivity : BaseActivity<CheckView, CheckPresenter>(), CheckView {

    private lateinit var adapter: CheckAdapter
    private lateinit var data: MutableList<CheckItem>
    private lateinit var carInfo: CarInfo
    private lateinit var checkInfo: CheckInfo

    override fun initInject() {
        getComponent().inject(this)
    }

    companion object {
        val PICTURE_STYLE = arrayOf("FRONT_LEFT", "FRONT_RIGHT", "REAR_LEFT", "REAR_RIGHT")
        val PICTURE_STYLE_NAME = arrayOf("前左45度", "前右45度", "后左45度", "后右45度")
        const val BEAN_TAG = "carInfo"
        fun jumpTo(context: Context, carInfo: CarInfo) {
            val intent = Intent(context, CheckActivity::class.java)
            intent.putExtra(BEAN_TAG, carInfo)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_check)
        initView()
    }

    private fun initView() {
        carInfo = intent.getParcelableExtra(BEAN_TAG)
        data = ArrayList()
        adapter = CheckAdapter(R.layout.check_rcv_item)
        PICTURE_STYLE_NAME.forEach {
            val checkItem = CheckItem()
            checkItem.name = it
            checkItem.status = 0
            data.add(checkItem)
        }
        adapter.addData(data)
        rcv_picture.layoutManager = GridLayoutManager(this, 2)
        rcv_picture.adapter = adapter
        rcv_picture.isNestedScrollingEnabled = false
        adapter.setOnItemClickListener { adapter, _, position ->
            if (adapter is CheckAdapter) {
                when (adapter.data[position].status) {
                    2 -> {
                        showToast("照片已经成功上传")
                        return@setOnItemClickListener
                    }
                    1 -> {
                        showToast("照片上传中")
                        return@setOnItemClickListener
                    }
                    0 -> {
                        PictureSelector.create(this)
                                .openGallery(PictureMimeType.ofImage())
                                .previewImage(false)
                                .maxSelectNum(1)
                                .forResult(position)
                    }
                    3 -> {
                        getPresenter().uploadPicture(carInfo, adapter.data[position].path!!, PICTURE_STYLE[position], position)
                    }
                }

            }
        }
        adapter.setOnItemChildClickListener { adapter, _, position ->
            if (adapter is CheckAdapter) {
                adapter.data[position].path = ""
                adapter.data[position].status = 0
                adapter.notifyDataSetChanged()
            }
        }
        initCheckInfo()
        //        getPresenter().makePicture(carInfo)
    }

    private fun initCheckInfo() {
        checkInfo = CheckInfo()
        sw_sfmj.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.sfmj = "是" else checkInfo.sfmj = "否"
        }
        sw_sfdk.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.sfdk = "是" else checkInfo.sfdk = "否"
        }
        sw_obdtxsfzc.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.obdtxsfzc = "是" else checkInfo.obdtxsfzc = "否"
        }
        sw_obdjcsfgz.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.obdjcsfgz = "是" else checkInfo.obdjcsfgz = "否"
        }
        sw_zxxtsfzc.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.zxxtsfzc = "是" else checkInfo.zxxtsfzc = "否"
        }
        sw_pqxtsfxl.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.pqxtsfxl = "是" else checkInfo.pqxtsfxl = "否"
        }
        sw_jcsfly.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.jcsfly = "是" else checkInfo.jcsfly = "否"
        }
        sw_sfgbtszz.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.sfgbtszz = "是" else checkInfo.sfgbtszz = "否"
        }
        sw_wgjcjg.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) checkInfo.wgjcjg = "1" else checkInfo.wgjcjg = "0"
        }
        edt_jczq.addTextChangedListener(object : MyTextWatcher {
            override fun afterTextChanged(s: Editable?) {
                checkInfo.jczq = s.toString()
            }
        })
        edt_obdgzm.addTextChangedListener(object : MyTextWatcher {
            override fun afterTextChanged(s: Editable?) {
                checkInfo.obdgzm = s.toString()
            }
        })
        edt_rlzl.isCursorVisible = false
        edt_rlzl.isFocusable = false
        edt_rlzl.isFocusableInTouchMode = false
        edt_rlzl.setOnClickListener {
            FuelChooseDialog(this, object : FuelChooseDialog.OnChoose {
                override fun choose(fuelType: String) {
                    checkInfo.rlzl = fuelType
                    edt_rlzl.setText(fuelType)
                }
            }).show()
        }
        btn_commit.setOnClickListener {
            getPresenter().uploadCheckInfo(carInfo, checkInfo)
        }
    }

    override fun onDestroy() {
        getPresenter().closeVideo(carInfo)
        super.onDestroy()
    }

    override fun onDataLoad(bean: String) {
        MyLog.i(bean)
    }


    override fun showException(throwable: Throwable) {
        super.showException(throwable)
        showToast("调用拍照或摄像头失败，检测退出")
        //        this.finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode in 0..3 && resultCode == RESULT_OK) {
            val selectList = PictureSelector.obtainMultipleResult(data)
            if (selectList.size == 1) {
                adapter.data[requestCode].path = selectList[0].path
                adapter.data[requestCode].status = 1
                adapter.notifyDataSetChanged()
                getPresenter().uploadPicture(carInfo, selectList[0].path, PICTURE_STYLE[requestCode], requestCode)
            }
        }
    }

    override fun onPictureUpload(position: Int) {
        adapter.data[position].status = 2
        adapter.notifyDataSetChanged()
    }

    override fun onPictureUploadFailed(position: Int, msg: String) {
        showToast(msg)
        adapter.data[position].status = 3
        adapter.notifyDataSetChanged()
    }
}
